package cm.tl.pms.util;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;

import java.util.HashMap;
import java.util.concurrent.CompletableFuture;

/**
 * @author w7h1te_ywq
 * @version 1.0.0
 * @date 2024-01-09 13:38
 */
public class SMSAliYunUtil {


    public static void main(String[] args) {
        //这个是图片的模版
        HashMap<String, Object> hashMap = sendSMS("15538222245", "1", "SMS_475305104");

        //这个久其报表登录的
//        HashMap<String, Object> hashMap = sendSMS("15538222245", "1","SMS_475310438");

    }

    /**
     * @param phoneNumber
     * @param phoneNumber 手机号
     * @param img         图片地址
     * @return
     */
    public static HashMap<String, Object> sendSMS(String phoneNumber, String img, String templateId) {
        HashMap<String, Object> hashMap = new HashMap<>();
        try {
            //参数，一定要对应短信模板中的参数顺序和个数，
            StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                    // Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
                    .accessKeyId("LTAI5tNZgnSgEwmPoQdmS92t")
                    .accessKeySecret("rZ2sYLFBG7XgppV0hbvvsx1sHYdzRR")
                    //.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // use STS token
                    .build());
            // Configure the Client
            AsyncClient client = AsyncClient.builder()
                    .region("cn-beijing") // Region ID
                    //.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient)
                    .credentialsProvider(provider)
                    //.serviceConfiguration(Configuration.create()) // Service-level configuration
                    // Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
                    .overrideConfiguration(
                            ClientOverrideConfiguration.create()
                                    // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
                                    .setEndpointOverride("dysmsapi.aliyuncs.com")
                            //.setConnectTimeout(Duration.ofSeconds(30))
                    )
                    .build();
            // Parameter settings for API request
            SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                    .signName("北京天成通链科技")
                    .templateCode(templateId)
                    .phoneNumbers(phoneNumber)
                    .templateParam("{\"img\":\"" + img + "\"}")
                    // Request-level configuration rewrite, can set Http request parameters, etc.
                    // .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
                    .build();
            // Asynchronously get the return value of the API request
            CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
            // Synchronously get the return value of the API request
            SendSmsResponse resp = response.get();
            System.out.println(new Gson().toJson(resp));
            // Asynchronous processing of return values

            client.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return hashMap;
    }

}